package com.dckeji.shiplock.webapp.service.impl;

import com.dckeji.shiplock.framework.constants.SystemConstants;
import com.dckeji.shiplock.framework.util.DateTimeUtil;
import com.dckeji.shiplock.webapp.dao.IOwnerDao;
import com.dckeji.shiplock.webapp.pojo.Crew;
import com.dckeji.shiplock.webapp.pojo.Owner;
import com.dckeji.shiplock.webapp.pojo.Route;
import com.dckeji.shiplock.webapp.pojo.UserInfo;
import com.dckeji.shiplock.webapp.service.IOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/8/11
 * Time: 10:10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OwnerServiceImpl implements IOwnerService {

    @Override
    public int ownerAuthentication(Owner owner) {
        Owner ownerInfo = ownerDao.queryOwnerInfo(owner);
        if (null == ownerInfo) {
            owner.setRegDate(DateTimeUtil.getSystemTime());
            Route route = new Route();
            route.setOpenId(owner.getOpenid());
            route.setRouteType(0);
            route.setShipNo(owner.getShipNo());
            int row = ownerDao.addRoute(route);
            return ownerDao.ownerAuthentication(owner);
        } else {
            return SystemConstants.ResultCode.UNIQUE;
        }
    }

    @Autowired
    IOwnerDao ownerDao;

    @Override
    public int addCrew(Crew crew) {
        return ownerDao.addCrew(crew);
    }

    @Override
    public List<Crew> queryCrewInfos(Integer ownerId) {
        return ownerDao.queryCrewInfos(ownerId);
    }

    @Override
    public int crewAuth(Crew crew) {
        Crew crew1 = ownerDao.queryCrewByPhone(crew);
        if (null != crew1) {
            int row = ownerDao.crewAuth(crew1);
            return row;
        } else {
            return SystemConstants.ResultCode.NODATE;
        }
    }


    @Override
    public UserInfo queryUserInfoByWechat(String openId) {
        List<Route> route1 = ownerDao.route(openId);
        if (null != route1 && route1.size() > 0) {
            Route route = route1.get(0);
            UserInfo userInfo = new UserInfo();
            userInfo.setShipNo(route.getShipNo());
            userInfo.setOpenid(userInfo.getOpenid());
            if (route.getRouteType() == 0) {
                //船长
                return ownerDao.queryOwnerInfo(userInfo);
            } else {
                //船员
                return ownerDao.queryCrewByOwner(userInfo);
            }
        } else {
            return null;
        }
    }

    @Override
    public int deleteCrew(Integer crewId) {


        return ownerDao.deleteCrew(crewId);
    }

    @Override
    public int updateCrew(Crew crew) {

        return ownerDao.updateCrew(crew);
    }

    @Override
    public Owner queryOwnerInfoByShipNo(String shipNo) {


        return ownerDao.queryOwnerInfoByShipNo(shipNo);
    }

    @Override
    public UserInfo queryUserInfo(String openId) {
        Route route = ownerDao.queryRouteByOpenId(openId);
        if (route.getRouteType() == 0) {
            return ownerDao.queryOwnerInfoByShipNo(route.getShipNo());
        } else {
            return ownerDao.queryCrewByOpenId(openId);
        }
    }

    @Override
    public int updatePwd(UserInfo userInfo) {

        Route route = ownerDao.queryRouteByOpenId(userInfo.getOpenid());
        if (route.getRouteType() == 0) {
            return ownerDao.updateOwnerpwd(userInfo);
        } else {
            return ownerDao.updateCrewPwd(userInfo);
        }
    }


}
